package com.qf.shopping.service.impl;

import com.qf.shopping.dao.BaseDao;
import com.qf.shopping.entity.Page;
import com.qf.shopping.service.BaseService;

import java.util.List;

/**
 * @author 三旬
 * @version 1.0  2022/9/14
 */

public abstract class BaseServiceImpl<T> implements BaseService<T> {
    /**
     * 继承的子类必须重写该方法，获得访问dao层的实例化对象
     * @return
     */
    protected abstract BaseDao<T> getBaseDao();
    @Override
    public Boolean add(T t) {
        return getBaseDao().insert(t) > 0;
    }

    @Override
    public Boolean deleteById(Integer id) {
        return getBaseDao().deleteById(id) > 0;
    }

    @Override
    public Boolean updateById(T t) {
        return getBaseDao().updateById(t) > 0;
    }

    @Override
    public T selectOneById(Integer id) {
        return getBaseDao().selectOneById(id);
    }

    @Override
    public List<T> selectAll() {
        return getBaseDao().selectAll();
    }

    @Override
    public void selectPage(Page<T> page) {
        //接收当前页面和页面显示的条数
        Integer index = page.getIndex();
        Integer size = page.getSize();
        //获得记录的总条数
        Integer counts = getBaseDao().counts();
        page.setCounts(counts);
        //获得页数
        if(counts%size == 0){
            page.setPages(counts/size);
        }else{
            page.setPages((counts/size) + 1);
        }
        //获得显示的数据
        List<T> selectPage = getBaseDao().selectPage((index - 1) * size, size);
        page.setList(selectPage);
    }

}
